package gd.qaq.blue.locationleader.api;

import gd.qaq.blue.locationleader.LocationLeader;
import gd.qaq.blue.locationleader.entity.TargetLocation;
import gd.qaq.blue.locationleader.listener.LocationLeaderListener;
import gd.qaq.blue.locationleader.util.pathfinder.PathUtils;
import gd.qaq.blue.locationleader.util.pathfinder.Tile;
import lk.vexview.api.VexViewAPI;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;

import static gd.qaq.blue.locationleader.LocationLeader.*;

public class API {
    public static void removeAllHuds(Player player) {
        VexViewAPI.removeHUD(player, "distanceText");
//        VexViewAPI.removeHUD(player, "distanceTextInfo");
        VexViewAPI.removeHUD(player, "directionIcon");
    }

    public static List<Location> getLocationPath(Player player, Location location) {
        Location start = player.getLocation().clone();
        while (start.getBlock().getType() == Material.AIR||start.getBlock().isLiquid()||start.getBlock().isEmpty()||start.getBlock().getType()==Material.GRASS) {
            start.setY(start.getY() - 1);
        }
        Location target = location.clone();
        while (target.getBlock().getType() == Material.AIR||target.getBlock().isLiquid()||target.getBlock().isEmpty()||target.getBlock().getType()==Material.GRASS) {
            target.setY(target.getY() - 1);
        }

        ArrayList<Tile> routes = PathUtils.createPath(start, target, 9999999);
        List<Location> locations = new ArrayList<>();
        if (routes != null) {
            for (Tile route : routes) {
                Location location1 = route.getLocation(player.getLocation());
                locations.add(location1);
            }
        }
        return locations;
    }

    public static void startAutoFindPath(Player player, TargetLocation targetLocation) {
        sendSubtitleToPlayer(player, colorFormat(config.getString("plugin.lang.autoFinding")), 9999999);
        List<Location> locationPath = getLocationPath(player, targetLocation.getLocation());
        targetLocation.setNextBlockIndex(0);
        targetLocation.setRoutes(locationPath);
        targetLocation.setLastUpdateTime(System.currentTimeMillis());
        targetLocation.setAuto(true);
        new BukkitRunnable() {
            private TargetLocation target = targetLocation;
            private Player targetPlayer = player;
            private int playerMovedCounts = 0;

            @Override
            public void run() {
                try {
                    List<Location> routes1 = this.target.getRoutes();
                    if (playerMovedCounts >= routes1.size() - 1 || !targetLocation.isAuto() || !player.isOnline()) {
                        targetPlayer.setVelocity(new Vector(0.1, 0, 0));
                        sendSubtitleToPlayer(player, colorFormat(""), 1);
                        cancel();
                        return;
                    }
                    Location target = routes1.get(playerMovedCounts);//下一个格子
                    target.setPitch(targetPlayer.getLocation().getPitch());
                    target.setYaw(targetPlayer.getLocation().getYaw());
                    targetPlayer.teleport(target);
                    targetPlayer.setVelocity(new Vector(0.1, 0, 0));
                    playerMovedCounts += 1;
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        }.runTaskTimer(LocationLeader.plugin, 0, 13L);
    }
}
